/******************************************************************************
* File description: CustomTextEdit.qml
* Function: 自定义多行文本框组件
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-07
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14

Item {
    id: root

    /* property 定义开始 */
    // 布局
    // 标签排列方式
    property int labelPosition: GLOBAL.LabelPosition.Top // 默认上下布局
    property int gridRowCount: _getGirdRowNCol(labelPosition)[0]  // 网格布局行数
    property int gridColCount: _getGirdRowNCol(labelPosition)[1] // 网格布局列数
    property int rowSpacing: 7
    property int colSpacing: 0

    // 标签
    property string textEditLabelFontFamily: "Alibaba PuHuiTi"
    property int textEditLabelFontPixelSize: 16
    property string textEditLabelColor: "#FF222222"
    property string textEditLabelText: "--EditLabel Default--"

    // textedit
    property int textEditBorderWidth: 1
    property string textEditBorderColor: "#FFBBBBBB"
    property int textEditRadius: 4
    property string textEditColor: "transparent"
    property string textEditFontFamily: "Alibaba PuHuiTi"
    property int textEditFontPixelSize: 16
    property string textEditFontColor: "#FF222222"

    // placeholder
    property string placeholderText: "--Default Placeholder--"
    property string placeholderColor: "#FF999999"

    // 计数区
    property string countAreaFontFamily: "Alibaba PuHuiTi"
    property int countAreaFontPixelSize: 16
    property string countAreaFontColor: "#FF999999"
    // 计数区文字对齐方式
    property int countAreaAlign: GLOBAL.CountAreaAlign.Right // 默认右对齐

    // 限定条件
    property int maxLength: 100 // 最大可输入字符数

    // 私有
    property int _maxLength: maxLength // 最大可输入字符数
    property int _currentLength: 0 // 当前字符数

    // 函数
    /*
      获取网格布局行列数
      @param {int} labelPosition 布局方式
      @return {list<int, int>} 网格布局行列数: [row, col]
     */
    property var _getGirdRowNCol: function(labelPosition){
        // 左右布局
        if(labelPosition === GLOBAL.LabelPosition.Left){
            return [1, 2]
        }
        // 上下布局
        else if(labelPosition === GLOBAL.LabelPosition.Top){
            return [2, 1]
        }
    }

    /* property 定义结束 */

    Item {
        anchors.fill: parent

        GridLayout {
            id: gridLayout
            anchors.fill: parent
            rows: gridRowCount
            columns: gridColCount
            rowSpacing: root.rowSpacing
            columnSpacing: root.colSpacing

            // 标签
            Item {
                id: baseLabelCon
                Layout.preferredWidth: baseLabel.width
                Layout.preferredHeight: baseLabel.height
                Layout.alignment: Qt.AlignVCenter | Qt.AlignLeft

                Label {
                    id: baseLabel
                    text: qsTr(textEditLabelText)
                    font.pixelSize: textEditLabelFontPixelSize
                    font.family: textEditLabelFontFamily
                    color: textEditLabelColor
                }
            }

            // 文本框
            Rectangle {
                id: textEditAreaContainer
                Layout.fillWidth: true
                Layout.fillHeight: true
                color: textEditColor
                border.width: textEditBorderWidth
                border.color: textEditBorderColor
                radius: textEditRadius

                ColumnLayout {
                    anchors.fill: parent
                    spacing: 0

                    Item {
                        id: textEditContainer
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        clip: true

                        Flickable {
                            id: flickable
                            anchors.fill: parent
                            contentWidth: textEdit.width
                            contentHeight: textEdit.height
                            boundsMovement: Flickable.StopAtBounds
                            // 滚动条
                            ScrollBar.vertical: CustomVerScrollBar {
                                id: scrollBar
                                height: textEdit.height / parent.height
                                anchors {
                                    top: parent.top
                                    right: parent.right
                                    rightMargin: 5
                                }
                            }

                            // 文本框
                            TextEdit {
                                id: textEdit
                                width: textEditContainer.width - scrollBar.width -
                                       scrollBar.anchors.rightMargin
                                height: contentHeight + topPadding > textEditContainer.height ?
                                            contentHeight + topPadding : textEditContainer.height
                                font.pixelSize: textEditFontPixelSize
                                font.family: textEditLabelFontFamily
                                color: textEditFontColor
                                topPadding: 12
                                leftPadding: 12
                                rightPadding: 12
                                wrapMode: TextEdit.Wrap
                                clip: true

                                // placeholder
                                Label {
                                    id: placeholder
                                    visible: textEdit.text.length === 0
                                    anchors {
                                        fill: parent
                                    }
                                    topPadding: parent.topPadding
                                    rightPadding: parent.rightPadding
                                    leftPadding: parent.leftPadding
                                    font: parent.font
                                    color: placeholderColor
                                    text: qsTr(placeholderText)
                                }

                                // 事件
                                // 监听字符变化
                                onTextChanged: {
                                    if(text.length > _maxLength) {
                                        textEdit.remove(_maxLength, length)
                                    }
                                    _currentLength = text.length
                                }
                            }
                        }
                    }

                    // 计数区
                    Item {
                        id: countArea
                        Layout.fillWidth: true
                        Layout.preferredHeight: 20

                        RowLayout {
                            anchors.fill: parent
                            anchors.leftMargin: 13
                            anchors.rightMargin: 13

                            CustomHorSpacerItem {visible: countAreaAlign === GLOBAL.CountAreaAlign.Right}

                            // 当前字符数
                            Label {
                                id: currentLength
                                Layout.fillHeight: true
                                font.family: countAreaFontFamily
                                font.pixelSize: countAreaFontPixelSize
                                color: countAreaFontColor
                                text: qsTr(_currentLength.toString())
                                verticalAlignment: Text.AlignVCenter
                            }

                            // 允许的字符数
                            Label {
                                id: validLength
                                Layout.fillHeight: true
                                font.family: countAreaFontFamily
                                font.pixelSize: countAreaFontPixelSize
                                color: countAreaFontColor
                                text: qsTr("/" + _maxLength.toString())
                                verticalAlignment: Text.AlignVCenter
                            }

                            CustomHorSpacerItem {visible: countAreaAlign === GLOBAL.CountAreaAlign.Left}
                        }

                        MouseArea {
                            anchors.fill: parent
                            cursorShape: Qt.IBeamCursor
                            onClicked: {
                                textEdit.forceActiveFocus()
                            }
                        }
                    }
                }
            }
        }
    }
}
